home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / modlib_s.lha / modlib_src / $retr.P < prev    next >
Text File  |  1990-04-12  |  5KB  |  126 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $retr.P */
  25.  
  26. /* retract routines */
  27.  
  28. $retr_export([$retract/1,$retr_abolish/1,$retr_abolish/2,$update/2,
  29.           $retractall/1]).
  30.  
  31. $retr_use($decompile,[_,$clause/3,_,_]).
  32.  
  33. /* $retr_use($bmeta,[$atom/1,$atomic/1,$integer/1,$number/1,$structure/1,
  34.     $functor0/2,$bldstr/3,$arg/3,$arity/2,$real/1,$float/1,_,_]).
  35.    $retr_use($buff,[$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,
  36.         $symtype/2,
  37.         $substring/6,$subnumber/6,$subdelim/6,$conlength/2,
  38.         $pred_undefined/1, $hashval/3]).
  39.    $retr_use($db,[$db_new_prref/1,$db_assert_fact/5,$db_add_clref/5,
  40.     $db_call_prref/2,$db_call_prref_s/2,$db_call_prref_s/3,
  41.     $db_call_clref/2,$db_get_clauses/3,$db_kill_clause/1]).
  42.    $retr_use($assert,[$assert/1,$asserti/2,$assert_union/2,$assert_call_s/1,
  43.         $assert_get_prref/2,$assert_put_prref/2,$assert_abolish_i/1]).
  44. */
  45.  
  46. /* this routine $retracts facts. It does so by running the chain of buffers,
  47.    explicitly. When it finds a fact that unifies, it overwrites the first
  48.    instruction in the buffer (after the retrymeelse instruction) to be a 
  49.    fail. This is somewhat of a kludge but is easy. Besides you shouldn't be
  50.    using $retract anyway. */
  51.  
  52. $retract(Clause) :-
  53.     $retr_chk_clause(Clause) ->
  54.          ((Clause = (Head :- Body) ->
  55.               true ;
  56.               (Clause = Head, Body = true)
  57.           ),
  58.           $clause(Head,Body,Ref),
  59.           $erase(Ref)
  60.          ) ;
  61.          ($telling(X), $tell(stderr),
  62.           $write('*** Error: illegal argument to retract/1: '),
  63.           $write(Clause), $nl,
  64.           $told, $tell(X)
  65.          ).
  66.  
  67. $retr_chk_clause(Cl) :- $atom(Cl).
  68. $retr_chk_clause(Cl) :-
  69.      $structure(Cl),
  70.      (Cl = (Hd :- Body) -> $retr_chk_hd(Hd) ; true).
  71.  
  72. $retr_chk_hd(Hd) :- $atom(Hd).
  73. $retr_chk_hd(Hd) :- $structure(Hd), Hd \= (_ :- _).
  74.  
  75. $retr_abolish(Goal) :- $buff_code(Goal,0,11,0).
  76. $retr_abolish(Pred,Arity) :-
  77.     ($atom(Pred), integer(Arity)) ->
  78.         ($functor(Term,Pred,Arity), $retr_abolish(Term)) ;
  79.         ($writename('*** abolish: illegal argument: '),
  80.          $write(Pred), $writename('/'), $write(Arity), $nl,
  81.          fail
  82.         ).
  83.  
  84. /* the following routines allow a tuple to be updated in a very limited way.
  85. Using these operations, you can change the value of an existing tuple in the
  86. database. The tuple must have been asserted, and only the first field
  87. can be changed and only if it is a constant or an integer. */
  88.  
  89. /* $upda_clref(Newval,Clref): Newval must be bound to a constant or integer, 
  90. and Clref must be bound to a clause reference. The first field of the tuple
  91. in Clref must be a constant or integer. This operation resets that first 
  92. field to Newval. */
  93.  
  94. $upda_clref(Newval,Clref) :- 
  95.     integer(Newval),!,
  96.     $buff_code(Clref,10,6 /*gb*/ ,14 /*getnumcon*/), /* right already */
  97.     $buff_code(Clref,12,2 /*pn*/ ,Newval).
  98.  
  99. $upda_clref(Newval,Clref) :- 
  100.     $atom(Newval),!,
  101.     $buff_code(Clref,10,6 /*gb*/ ,4 /*getcon*/), /*must already be right*/
  102.     $buff_code(Clref,12,1 /*pppsc*/ ,Newval).
  103.  
  104. $update(Fact,Newval) :-
  105.     $assert_get_prref(Fact,Prref),$db_call_prref_s(Fact,Prref,Clref),
  106.     $upda_clref(Newval,Clref).
  107.  
  108. $retractall(Hd) :-
  109.      $retr_chk_hd(Hd) ->
  110.           $retractall1(Hd) ;
  111.       ($telling(X), $tell(stderr),
  112.        $write('*** Error: illegal argument to retractall/1: '),
  113.        $write(Hd), $nl,
  114.        $told, $tell(X)
  115.       ).
  116.  
  117. $retractall1(Hd) :-
  118.      $clause(Hd,_,Ref),
  119.      $erase(Ref),
  120.      fail.
  121. $retractall1(_).
  122.  
  123.  
  124. /* ------------------------------ $retr.P ------------------------------ */
  125.  
  126.